<template>
  <div class="xtx-checkbox" @click="changeChecked">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span v-if="$slots.default"><slot></slot></span>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
  export default {
    name: 'XtxCheckbox',
    props:{
      modelValue:{
        type:Boolean,
        default:false
      }
    },
    setup(props,{ emit }) {
      const checked = ref(false)
      const changeChecked = () => {
        checked.value = !checked.value
        // 使用emit通知父组件数据的改变
        emit('update:modelValue',checked.value)
        emit('change',checked.value)
      }
      // 使用侦听器，得到父组件传递数据，给checked数据
      watch(()=>props.modelValue,(newValue) => {
          checked.value = newValue
      },{immediate:true})
      return {
        checked,
        changeChecked
      }
    }
  }
</script>

<style lang="less" scoped>
  .xtx-checkbox {
  display: inline-block;
  margin-right: 2px;
  .icon-checked {
    color: @xtxColor;
    ~ span {
      color: @xtxColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>